﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Admin.SystemSet
{
    public partial class NewsTypeManager : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadContent(false);

                // 从BasePage中得到
                PageLiteral.Text = BuildPageLinks();
            }
        }


        #region 加载列表
        private void LoadContent(bool mark)
        {

            //设置控件属性，从BasePage中得到
            PageConfig(NewsTypeUPager);


            //List<Member> list = new List<Member();
            NewsTypeHelper nthelper = new NewsTypeHelper();

            NewsTypeUPager.ItemCount = nthelper.QueryNewsTypeCountByAll();

            // 以下的值从BasePage中得到
            NewsTypeUPager.UrlFormat = UrlFormat;
            NewsTypeUPager.PrefixText = GetPreFixText(NewsTypeUPager);

            if (mark)
            {
                PagerInit(NewsTypeUPager);
            }

            string[] fields = new string[] { "ID", "NewsTypeName", "CreateDate" };

            DataTable dt = new DataTable();
            dt = nthelper.QueryNewsTypeBySearchToTable(null, new Order[] { new Order("CreateDate",OrderMode.Desc) }, NewsTypeUPager.Begin - 1, NewsTypeUPager.Count, fields);

            rptNewsType.DataSource = dt;
            rptNewsType.DataBind();
        }
        #endregion

        #region 列表事件
        protected void rptNewsType_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            TextBox tbNewsTypeName = (TextBox)e.Item.FindControl("txtNewsTypeName");
            
            string id = hidID.Value;

            
            if (e.CommandName == "edit")//编辑
            {
                string newstypename = tbNewsTypeName.Text.Trim();
                if (string.IsNullOrEmpty(newstypename))
                {
                    Tools.Alert("请输入类别名称！");
                    return;
                }
                NewsType ntype = new NewsType();
                NewsTypeHelper nthelper = new NewsTypeHelper();
                ntype = nthelper.GetNewsType(id);
                ntype.ID = id;
                ntype.NewsTypeName = newstypename;
                nthelper.EditNewsType(ntype);

                Tools.Alert("修改成功！");
            }
            else if (e.CommandName == "delete")//删除
            {
                NewsType ntype = new NewsType();
                NewsTypeHelper nthelper = new NewsTypeHelper();
                ntype = nthelper.GetNewsType(id);
                nthelper.DeleteNewsType(ntype);
                Tools.Alert("删除成功！");
            }

            LoadContent(false);
        }
        #endregion

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("NewsTypeAdd.aspx");
        }
    }
}